﻿using System;
using System.Collections.Generic;


using UnityEngine;

public class FrameAnimation : MonoBehaviour
{

    public bool loop = true;                    // 循环播放
    public float elapseTime = 0.5f;             // 播放速度
    public List<GameObject> images = new List<GameObject>();                // 图集

    public Action callbackFinished = null;


    protected int idx = 0;
    protected float time = 0;

    void Start()
    {
        foreach( var item in images )
        {
            item.SetActive(false);
        }

        images[0].SetActive(true);
    }


    void Update()
    {
		time += Time.unscaledDeltaTime;

        if ( time >= elapseTime )
        {

            GetNowImage().SetActive(false);
            ToNextImage().SetActive(true);

            time = 0;


            // 循环完成 回调
            if (idx == images.Count - 1 && !loop)
            {

                if (callbackFinished != null)
                    callbackFinished();
            }

        }




    }

    protected GameObject GetNowImage()
    {
        return images[idx];
    }

    protected GameObject ToNextImage()
    {
        if (idx >= images.Count -1)
            idx = 0;
        else
            idx++;

        return images[idx];
    }

    protected GameObject GetNextImage()
    {
        if (idx >= images.Count)
            return images[0];
        else
            return images[idx + 1];
    }

}

